import allNewsData from '~/pages/Data/news.json'
import { NEWS_SORT } from '~/pages/Doc/sort'
import { useI18n } from 'vue-i18n';

export interface IMenu {
  key: string
  label: string
  url: string
}

/**
 * 获取菜单数据
*/
export const useMenuData = () => {
  const { t } = useI18n()
  const labelKeyReg = /[a-zA-Z-_]+/g
  const activityMenu = ref([{ key: 'activity', label: t('common.activity'), url: '/activity' }])
  const router = useRouter()
  // 获取cookie里参数
  const lan = useCookie('lang').value || 'zh'
  const lang = (lan === 'en' ? 'en' : 'zh') as 'en' | 'zh'
  const newsData: any = (allNewsData as any)[lang] || (allNewsData as any).zh
  const sortList = NEWS_SORT[lang]

  /** 菜单数据（响应式） */
  const allColumns = computed(() => Object.keys(newsData)) // 获取所有专栏的集合

  const speList = computed(() => ([
    ...sortList.filter((item: string) => newsData[item]),
    ...allColumns.value.filter((item: string) => !sortList.includes(item)),
  ]))

  const newMenu = computed(() => getNewMenuData(speList.value))

  const menus = computed<Array<IMenu>>(() => {
    const arr: Array<IMenu> = [
      { key: 'third-party', label: t('common.thirdParty'), url: '/third-party' },
      ...activityMenu.value,
      ...newMenu.value,
      { key: 'course', label: t('common.course'), url: '/course' },
      { key: 'ebook', label: t('common.ebook'), url: '/ebook' },
      { key: 'community', label: t('common.community'), url: '/community' },
    ]
    return arr
  })
  /** 根据中文名生成key */
  function getLabelKeys(labels: string[]) {
    labels = labels.map((item) => item.toLowerCase())
    const labelKeys: string[] = []
    for (let i = 0; i < labels.length; i++) {
      const label = labels[i]
      if (labelKeyReg.test(label)) {
        const key = label.match(labelKeyReg)?.[0] || i.toString()
        if (labelKeys.includes(key)) {
          labelKeys.push(label + '_' + i)
        } else {
          labelKeys.push(key)
        }
        continue
      }

      const labelKey = label.split('').map(char => char.charCodeAt(0).toString(36)).join('')
      if (labelKeys.includes(labelKey)) {
        labelKeys.push(label + '_' + i)
      } else {
        labelKeys.push(labelKey)
      }
    }
    return labelKeys
  }

  /**提取菜单 并生成url */
  function getNewMenuData(speList: string[]) {
    const keys = getLabelKeys(speList)
    return speList.map((key, index) => ({
      key: keys[index],
      label: key,
      url: `/news?nav=${keys[index]}`
    }))
  }

  /** 获取默认展示的nav */
  function getDefaultNav() {
    if ('third-party' === router.currentRoute.value.name) {
      return 'third-party'
    }
    if ('activity' === router.currentRoute.value.name) {
      return 'activity'
    }
    if ('course' === router.currentRoute.value.name) {
      return 'course'
    }
    if ('ebook' === router.currentRoute.value.name) {
      return 'ebook'
    }
    if ('community' === router.currentRoute.value.name) {
      return 'community'
    }
	if ('thirdParty' === router.currentRoute.value.name) {
		return 'thirdParty'
	}
    const { nav } = router.currentRoute.value.query
    if (nav && menus.value.some(item => item.key === nav)) {
      return nav
    }
    return 'g-star'
  }

  /** 默认显示的key */
  const defaultNav = ref(getDefaultNav())
  return {
    menus,
    newMenu,
    activityMenu,
    defaultNav,
    getLabelKeys,
    getDefaultNav
  }
}
